package it.unisa.unibox.gestioneProdotti;



import it.unisa.unibox.facade.FacadeProdotti;
import it.unisa.unibox.facade.IFacadeProdotti;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class VisualizzaProdottoServlet
 */
public class StampaProdottoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public static ServletContext servletContext;

	private String filePath;
	     

	public static IFacadeProdotti facade;


	public StampaProdottoServlet() {
		super();
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		try {
			super.init(config);			
			servletContext = getServletConfig().getServletContext();
			filePath = servletContext.getRealPath("files");
			facade = new FacadeProdotti();

		} 

		catch (Exception e) {
			e.printStackTrace();
		}
	}


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request,response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setHeader("Cache-Control",	"no-store, no-cache, must-revalidate");
		PrintWriter out;
		out = response.getWriter();

		int id = Integer.parseInt(request.getParameter("id"));
				
		String url = facade.stampaProdotto(id);
		if(url!=null)
			out.write(url);
		else
			out.write("error");
		
	}
	

}
